package adapter

import (
	"github.com/stretchr/testify/assert"
	"google.golang.org/grpc"
	"migration-tool/common"
	"testing"
)

func TestCodeupDealDeployKeySetting(t *testing.T) {
	cfg, err := common.ParseConfig(TestDefaultConfigPath)
	assert.Equal(t, err, nil)
	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	conn, err := grpc.Dial(DefaultRpcHost, grpc.WithInsecure())
	assert.Equal(t, err, nil)
	defer conn.Close()
	args := TaskArgs{
		RepoInfo:           &repoInfo,
		Conn:               conn,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: AonePlatformName,
		Log:                log,
	}

	err = codeupDealDeployKeysSetting(args)
	assert.Equal(t, err, nil)
}

func TestCodeupDealWebHookSetting(t *testing.T) {
	cfg, err := common.ParseConfig(TestDefaultConfigPath)
	assert.Equal(t, err, nil)
	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	conn, err := grpc.Dial(DefaultRpcHost, grpc.WithInsecure())
	assert.Equal(t, err, nil)
	defer conn.Close()
	args := TaskArgs{
		RepoInfo:           &repoInfo,
		Conn:               conn,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: AonePlatformName,
		Log:                log,
	}

	err = codeupDealWebHookSetting(args)
	assert.Equal(t, err, nil)
}

func TestCodeupDealPushRuleSetting(t *testing.T) {
	cfg, err := common.ParseConfig(TestDefaultConfigPath)
	assert.Equal(t, err, nil)
	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	conn, err := grpc.Dial(DefaultRpcHost, grpc.WithInsecure())
	assert.Equal(t, err, nil)
	defer conn.Close()
	args := TaskArgs{
		RepoInfo:           &repoInfo,
		Conn:               conn,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: AonePlatformName,
		Log:                log,
	}

	err = codeupDealPushRuleSetting(args)
	assert.Equal(t, err, nil)
}

func TestCodeupDealCodeReviewSetting(t *testing.T) {
	cfg, err := common.ParseConfig(TestDefaultConfigPath)
	assert.Equal(t, err, nil)
	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	conn, err := grpc.Dial(DefaultRpcHost, grpc.WithInsecure())
	assert.Equal(t, err, nil)
	defer conn.Close()
	args := TaskArgs{
		RepoInfo:           &repoInfo,
		Conn:               conn,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: AonePlatformName,
		Log:                log,
	}

	err = codeupDealCodeReviewSetting(args)
	assert.Equal(t, err, nil)
}

func TestCodeupDealProtectBranchSetting(t *testing.T) {
	cfg, err := common.ParseConfig(TestDefaultConfigPath)
	assert.Equal(t, err, nil)
	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	conn, err := grpc.Dial(DefaultRpcHost, grpc.WithInsecure())
	assert.Equal(t, err, nil)
	defer conn.Close()
	args := TaskArgs{
		RepoInfo:           &repoInfo,
		Conn:               conn,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: AonePlatformName,
		Log:                log,
	}

	err = codeupDealProtectBranchSetting(args)
	assert.Equal(t, err, nil)
}

func TestCodeupDealRepoMetaInfo(t *testing.T) {
	cfg, err := common.ParseConfig(TestDefaultConfigPath)
	assert.Equal(t, err, nil)
	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	conn, err := grpc.Dial(DefaultRpcHost, grpc.WithInsecure())
	assert.Equal(t, err, nil)
	defer conn.Close()
	args := TaskArgs{
		RepoInfo:           &repoInfo,
		Conn:               conn,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: AonePlatformName,
		Log:                log,
	}

	err = codeupDealRepoMetaInfo(args)
	assert.Equal(t, err, nil)
}

func TestCodeupDealRepoMember(t *testing.T) {
	cfg, err := common.ParseConfig(TestDefaultConfigPath)
	assert.Equal(t, err, nil)
	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	conn, err := grpc.Dial(DefaultRpcHost, grpc.WithInsecure())
	assert.Equal(t, err, nil)
	defer conn.Close()
	args := TaskArgs{
		RepoInfo:           &repoInfo,
		Conn:               conn,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: AonePlatformName,
		Log:                log,
	}

	err = codeupDealRepoMember(args)
	assert.Equal(t, err, nil)
}

func TestCodeupDealMergeRequest(t *testing.T) {
	cfg, err := common.ParseConfig(TestDefaultConfigPath)
	assert.Equal(t, err, nil)
	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	conn, err := grpc.Dial(DefaultRpcHost, grpc.WithInsecure())
	assert.Equal(t, err, nil)
	defer conn.Close()
	args := TaskArgs{
		RepoInfo:           &repoInfo,
		Conn:               conn,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: AonePlatformName,
		Log:                log,
	}

	err = codeupDealMergeRequest(args)
	assert.Equal(t, err, nil)
}
